
var TextEditor = function() {
  this.current = 0;
  this.text = "";
};

/**
 * @param {string} text
 * @return {void}
 */
TextEditor.prototype.addText = function(text) {
  let leftText = this.text.substring(0, this.current);
  let rightText = this.text.substring(this.current, this.text.length);
  this.text = leftText + text + rightText;
  this.current = this.current + text.length;
};

/**
 * @param {number} k
 * @return {number}
 */
TextEditor.prototype.deleteText = function(k) {
  let leftIndex = this.current - k < 0 ? 0 : this.current - k;
  let leftText = this.text.substring(0, leftIndex);
  let deleteText = this.text.substring(leftIndex, this.current);
  let rightText = this.text.substring(this.current, this.text.length);
  this.current = leftIndex;
  this.text = leftText + rightText;
  return deleteText.length;
};

/**
 * @param {number} k
 * @return {string}
 */
TextEditor.prototype.cursorLeft = function(k) {
  this.current = this.current - k;
  if(this.current < 0) this.current = 0;
  // 左边还有的长度
  let num = this.text.substring(0, this.current).length;
  num = Math.min(num, 10);
  let leftText = this.text.substring(this.current - num, this.current);
  // console.log(`移动左侧${k}个位置,text为${this.text},当前光标${this.current}`);
  return leftText;
};

/**
 * @param {number} k
 * @return {string}
 */
TextEditor.prototype.cursorRight = function(k) {
  this.current = this.current + k;
  if(this.current > this.text.length) this.current = this.text.length;
  let num = this.text.substring(0, this.current).length;
  num = Math.min(num, 10);
  let rightText = this.text.substring(this.current - num, this.current);
  // console.log(`移动右侧${k}个位置,text为${this.text},当前光标${this.current}`);
  return rightText;
};

// function operator(operations, params) {
//   let textObj = new TextEditor();
//   let result = [[]];
//   for(let i = 1; i < operations.length; i++) {
//     result.push(textObj[operations[i]](params[i][0]));
//   }
//   console.log(result);
// }
// operator(["TextEditor","addText","cursorLeft","deleteText","cursorLeft","addText","cursorRight"],
// [[],["bxyackuncqzcqo"],[12],[3],[5],["osdhyvqxf"],[10]]);
// operator(["TextEditor","addText","cursorLeft","cursorLeft","addText","deleteText"],
// [[],["jxarid"],[5],[10],["du"],[20]]);
// operator(["TextEditor","addText","cursorLeft","deleteText","cursorLeft","addText","cursorRight"],
// [[],["bxyackuncqzcqo"],[12],[3],[5],["osdhyvqxf"],[10]]);


